﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Millionaire;
using WindowsFormsApplication1.BLL;

namespace WindowsFormsApplication1
{
    public partial class FrmGame : Form
    {
        private NguoiChoi nguoiChoi;
        private List<int> linhVuc;
        private int soThuTu = 1;
        private CauHoiBLL cauHoiBLL;
        private LuotChoiBLL luotChoiBLL;
        private ChiTietLuotChoiBLL chiTietBLL;
        private LuotChoi lc;
        private string dapAn = "";
        private int currCauHoi;
        
        private bool tatCaLV = false;
       
        public FrmGame(List<int> linhVuc, NguoiChoi nc)
        {
            InitializeComponent();
            this.nguoiChoi = nc;
            if (linhVuc.Contains(0))
                tatCaLV = true;
            else
                this.linhVuc = linhVuc;
            cauHoiBLL = new CauHoiBLL();
            luotChoiBLL = new LuotChoiBLL();
            chiTietBLL = new ChiTietLuotChoiBLL();
        }

        private void FrmGame_Load(object sender, EventArgs e)
        {
            lc = new LuotChoi()
            {
                MaNguoiChoi = nguoiChoi.MaNguoiChoi,
                Ngay = DateTime.Now                
            };
            luotChoiBLL.AddNew(lc);
            DisplayCauHoi();
        }
        
        private void DisplayCauHoi()
        {
            lblSoTT.Text = "" + soThuTu;
             CauHoi ch;
            if(tatCaLV)
                ch = cauHoiBLL.RandomMD( soThuTu);
            else
                ch = cauHoiBLL.RandomNhieuLV(linhVuc, soThuTu);
            if (ch == null)
            {
                MessageBox.Show("Hết câu hỏi");
                return;
            }
            currCauHoi = ch.MaCauHoi;
            lblCauHoi.Text = ch.CauHoi1;
            rdCauA.Text = "A. " + ch.CauA;
            rdCauB.Text = "B. " + ch.CauB;
            rdCauC.Text = "C. " + ch.CauC;
            rdCauD.Text = "D. " + ch.CauD;
            dapAn = ch.DapAn;
            ChiTietLuotChoi chitiet = new ChiTietLuotChoi()
            {
                MaCauHoi = ch.MaCauHoi,
                MaLuotChoi = lc.MaLuotChoi,
                STTCauHoi = soThuTu              
            };
            chiTietBLL.AddNew(chitiet);
            SetEnable();
        }

        private void btnTraLoi_Click(object sender, EventArgs e)
        {
            bool rs = false;
            switch (dapAn)
            {
                case "A":
                    {
                        if (rdCauA.Checked) rs = true;
                        break;
                    }
                case "B":
                    {
                        if (rdCauB.Checked) rs = true;
                        break;
                    }
                case "C":
                    {
                        if (rdCauC.Checked) rs = true;
                        break;
                    }
                case "D":
                    {
                        if (rdCauD.Checked) rs = true;
                        break;
                    }
            }
            if (rs)
            {
                soThuTu++;
                DisplayCauHoi();
            }
            else
            {
                MessageBox.Show("Trả lời sai. Đáp án đúng là : "+dapAn);
                MessageBox.Show("Bạn đã trả lời đúng tất cả " + (soThuTu - 1) + " câu");
                this.Close();
            }
        }

        private void btnDoiCauHoi_Click(object sender, EventArgs e)
        {
            CauHoi ch;
            if (tatCaLV)
                ch = cauHoiBLL.DoiCauHoi(soThuTu,currCauHoi);
            else
                ch = cauHoiBLL.DoiCauHoi(linhVuc,soThuTu, currCauHoi);
            if (ch == null)
            {
                MessageBox.Show("Hết câu hỏi");
                return;
            }
           
            lblCauHoi.Text = ch.CauHoi1;
            rdCauA.Text = "A. " + ch.CauA;
            rdCauB.Text = "B. " + ch.CauB;
            rdCauC.Text = "C. " + ch.CauC;
            rdCauD.Text = "D. " + ch.CauD;
            dapAn = ch.DapAn;
            ChiTietLuotChoi chitiet = chiTietBLL.GetByCauHoiLuotChoi(currCauHoi, lc.MaLuotChoi);
            chitiet.MaCauHoi = ch.MaCauHoi;
            chiTietBLL.Update(chitiet);
            currCauHoi = ch.MaCauHoi;
            chiTietBLL.AddNew(chitiet);
            btnDoiCauHoi.Enabled = false;
            SetEnable();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dapAn == "A")
            {
                rdCauB.Enabled = false;
                 rdCauD.Enabled = false;
            }
            else if (dapAn == "B")
            {
                rdCauA.Enabled = false;
                rdCauC.Enabled = false;
            }
            else if (dapAn == "C")
            {
                rdCauB.Enabled = false;
                rdCauA.Enabled = false;
            }
            else
            {
                rdCauB.Enabled = false;
                rdCauC.Enabled = false;
            }
            button2.Enabled = false;
        }

        void SetEnable()
        {                   
            rdCauB.Enabled = true;
            rdCauD.Enabled = true;
            rdCauC.Enabled = true;
            rdCauA.Enabled = true;
        }
    }
}
